﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SystemManage_Labor_CompanyEdit : GH.Web.BasePage.PageBase
{


    #region 画面初始化

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //文化程度
            Common.SetDataBound(ddlStyle, "201");
            //添加单位
            if (Request.QueryString["Action"] == "Add")
            {
                this.txtID.Value = "";
                SetControlsState(enumAction.InsertAction);
            }
            //更新单位
            else if (Request.QueryString["Action"] == "Update")
            {
                SetControlsState(enumAction.UpdateAction);
            }
            //更新单位
            else if (Request.QueryString["Action"] == "Del")
            {
                SetControlsState(enumAction.DeleteAction);
            }
        }
    }

    #endregion


    #region 画面方法

    private void SetControlsState(enumAction enumAction)
    {
        switch (enumAction)
        {
            case enumAction.InsertAction:

                txtComCode.Text.Trim();
                txtComName.Text.Trim();
                txtComAddress.Text.Trim();
                break;
            case enumAction.UpdateAction:
                //CheckRight(403);
                GH.BLL.Union unionBll = new GH.BLL.Union();
                GH.Model.工会单位信息 mdlUnionInfo = new GH.Model.工会单位信息();
                mdlUnionInfo = unionBll.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
                this.txtID.Value = Request.QueryString["ID"].ToString();
                txtComCode.Text = mdlUnionInfo.单位代码;
                txtComName.Text = mdlUnionInfo.单位名称;
                ddlStyle.SelectedValue = mdlUnionInfo.类型;
                txtComAddress.Text = mdlUnionInfo.地址;
                break;
            case enumAction.DeleteAction:
                //CheckRight(404);
                UnionOpration();
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// 单位登录,更新,删除
    /// </summary>
    public void UnionOpration()
    {
        GH.BLL.Union unionBll = new GH.BLL.Union();
        GH.Model.工会单位信息 mdlUnionInfo = new GH.Model.工会单位信息();
        mdlUnionInfo.单位代码 = txtComCode.Text.Trim();
        mdlUnionInfo.单位名称 = txtComName.Text.Trim();
        mdlUnionInfo.类型 = ddlStyle.SelectedValue;
        mdlUnionInfo.地址 = txtComAddress.Text.Trim();

        //添加单位
        if (Request.QueryString["Action"] == "Add")
        {
            try
            {
                unionBll.Add(mdlUnionInfo);
                AlertMessage("添加单位成功");
                JavascriptGoUrl("Labor_CompanyManage.aspx");
            }
            catch
            {
                AlertMessage("添加单位失败");
            }
        }
        //更新单位
        else if (Request.QueryString["Action"] == "Update")
        {
            try
            {
                mdlUnionInfo.ID = int.Parse(Request.QueryString["ID"].ToString());
                unionBll.Update(mdlUnionInfo);
                AlertMessage("更新单位成功");
                JavascriptGoUrl("Labor_CompanyManage.aspx");
            }
            catch
            {
                AlertMessage("更新单位失败");
            }

        }
        //删除单位
        else if (Request.QueryString["Action"] == "Del")
        {
            try
            {
                mdlUnionInfo.ID = int.Parse(Request.QueryString["ID"].ToString());
                unionBll.Delete(mdlUnionInfo.ID);
                AlertMessage("删除单位成功");
                //JavascriptGoBack();
                Response.Redirect(Request.ServerVariables["http_referer"]);

            }
            catch
            {
                AlertMessage("删除单位失败");
                JavascriptGoBack();
            }
        }
    }


    #endregion

    #region 画面事件

    protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        UnionOpration();
    }

    #endregion
}
